from app.api import abstract

class Action(abstract.Action):

    def _post(self):
        
        # save user name
        if self.param("username") != "" and self.param("username") != None:
            abstract.models.user.setName(self.viewer_id, self.param("username"))

        wish = abstract.models.wish.add(
            self.viewer_id,
            self.param("title"),
            self.param("description"),
            self.param("link"),
            self.param("reason_id", "int"),
            self.param("category_id", "int"),
            self.param("price_from", "int"),
            self.param("price_to", "int"),
            self.param("img_url"),
            self.param("tags"),
            self.platform_id,
        )
        
        if not wish: return False

        wish_ids = abstract.utils.id2ids(wish.key().id())
        
        if wish.image != None:
            img_url = abstract.config.IMG_URL + "?id=" + wish_ids + "&nc=" + wish.updated.strftime("%d%m%y%H%M%S")
        else:
            img_url = ""
            
        if wish.reserver_id == None:
            reserved = False
        else: 
            reserved = True
            
        wish.title = abstract.utils.xssescape(wish.title)
        if wish.description != None: wish.description = abstract.utils.xssescape(wish.description)
        wish.link = abstract.utils.xssescape(wish.link)
        wish.tags = [abstract.utils.xssescape(t) for t in wish.tags]
        
        data = {"wish_id" : wish_ids, "price_from" : wish.price_from, "price_to" : wish.price_to, "reserved" : reserved, "title" : wish.title, 
                   "link" : wish.link , "img_url" : img_url, "tags" : wish.tags, "owner_id" : wish.owner_id, "description" : wish.description,
                   "reason_id" : wish.reason_id, "status" : wish.status, "updated" : wish.updated.strftime("%d.%m.%y %H:%M:%S"),
                   "likes" : wish.likes, "likers" : wish.likers, "category_id" : wish.category_id}
            
        return data